package com.timber.util;

import java.util.*;
import java.text.*;
import java.sql.*;
//import javax.swing.JTree;
//import javax.swing.tree.*;
import java.lang.*;

import pv.jfcx.*;

/**
 *	object to hold row values in an array of cells( Object reference )
 * 	array size is determined by the number of columns in ColumnManager
 *	load values from the database record using load(...) method
 *	based on column definitions in the report
 *	colMgr MUST be set to read data from the database
 *
 */
public class GenericRow extends SLDataObjectRowAdaptor implements Cloneable
{
  protected SLTableModel tModel = null;
  protected SLColumnManager colMgr;
  protected Object cells[] = null;

  public GenericRow()
  {}

  public GenericRow(SLTableModel tm)
  {
    setUpRow( tm );
  }

  /**
   * allocate cell array to hold row data
   * @param cm GenericColumnManager
   */
  public GenericRow( SLColumnManager cm )
  {
    colMgr = cm;
    clear();
  }

  /**
   * return the data size in chars
   * @param obj Object
   * @return int
   */
  public static int calcFieldSize(Object obj)
  {
    if( obj == null )
      return 0;

    if( obj instanceof java.util.Date)
      return 10;
    else
      return obj.toString().trim().length() + 2;
  }


  /**
   *	allocate array to hold cell values
   */
  public void setUpRow(SLTableModel tm)
  {
    tModel = tm;
    colMgr = tm.getColumnManager();
    clear();
  }

  /**
   *	allocate array of cells
   *	when tableModel is set, use masterColumnCount for size
   *	else...
   *	try colMgr.getColumnCount,  this happens when coming from generic data source( report )
   */
  public void clear()
  {
    cells = ( tModel != null ? new Object[ Math.max(tModel.getMasterColumnCount(),tModel.getColumnCount()) ] : null );
    if( cells == null )
    cells = ( colMgr != null ? new Object[ colMgr.getColumnCount() ] : null );
  }

  /**
   *	load current row values from ResultSet into cells array
   *	overrides load method in SLDataMgr
   */
  public boolean load(ResultSet rs) throws SQLException, IllegalStateException
  {
    String name;
    Object obj = null;
    SLColumnDefinition rcdef = null;

    //	load fields from database record
    for(Iterator iter = colMgr.getColumnDefs().iterator(); iter.hasNext(); )
    {
      rcdef = (SLColumnDefinition) iter.next();

      if ( ! rcdef.isInResSet() ) continue;

      name = (String) rcdef.getIdName();
      obj  = rs.getObject(name);
      cells[ rcdef.getId() ] = obj;

      // calc max column width based on data
      int size = calcFieldSize( obj );
      if (rcdef.getDisplayWidth() < size)
	rcdef.setDisplayWidth(size);

    }

    return true;
  }

  /**
   *	return value based on column name
   */
  public Object getField(String name)
  {
    try{
      SLColumnDefinition rcd = (SLColumnDefinition) colMgr.getByIdName(name);
      return cells[ rcd.getId() ];
    }catch(Exception ex) {
      return null;
    }
  }

  /**
   *	return value based on column index
   */
  public Object getField(int colId)
  {
    try{
      return cells[ colId ];
    }catch(Exception ex) {
      return null;
    }
  }

  /**
   * set cell for specified column name
   */
  public void setField( String name, Object o)
  {
    SLColumnDefinition rcd = (SLColumnDefinition) colMgr.getByIdName(name);
    if( rcd != null )
      cells[ rcd.getId() ] = o;
    else
      Util.error( "setField", "Attempting to set undefined column: <" + name + ">");
  }

  /**
   *	set cell for specified column id
   */
  public void setField(int colId, Object o) {
    cells[colId] = o;
  }

  public void setTableModel( SLTableModel tm )
  {
    tModel = tm;
    colMgr = tm.getColumnManager();
    clear();
  }

  /**
   *	clone row object
   */
  public Object clone() throws CloneNotSupportedException
  {
    return super.clone();
  }

  public void setColumnManager(SLTableModel tm)
  {
    setUpRow( tm );
  }

  public void setColumnManager( SLColumnManager cm )
  {
    colMgr = cm;
    clear();
  }

  public int size()
  {
  	if( cells != null )
  		return cells.length;
  	return 0;
  }
}
